package controller;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import dto.MemberDTO;
import dto.ReservationDTO;
import dto.RoomDTO;

import service.ReservationService;

@Controller
public class ReservationController {
	private ReservationService service;
	public ReservationController(ReservationService service){
		this.service = service;
	}
	//예약등록
	@RequestMapping("/reservation_register.do")
	public ModelAndView registerReservation(ReservationDTO redto, String memberId) throws Exception{
		redto.setReservationNo(service.getReservationNo());
		redto.setMemberId(memberId);
		redto.setReservationCode("W");
		try{
			service.registerReservation(redto);
		}catch(Exception e){
			e.printStackTrace();
			return new ModelAndView("error.res","error_message", e.getMessage());
		}
		return new ModelAndView("reservation_success.res");
	}
	//register.jsp 이동
	@RequestMapping("goRegisterjsp.do")
	public String goRegisterjsp(){		
		return "reservation_register.res";
	}
	//나의예약정보 - 여행자
	@RequestMapping("my_reservation_traveler_info.do")
	public ModelAndView selectMyReservation(String memberId) throws Exception{
		try{
			ArrayList list = service.selectReservation(memberId);
			return new ModelAndView("my_reservation_traveler.sub", "list", list);
		}catch(Exception e){
			e.printStackTrace();
			return new ModelAndView("error.res","error_message", e.getMessage());
		}
	}
	//예약상세정보
	@RequestMapping("reservation_info_detail.do")
	public ModelAndView selectReservationDetail(int roomNo, int reservationNo) throws Exception{
		try{
			HashMap map = new HashMap();
			map.put("roomNo", roomNo);
			map.put("reservationNo", reservationNo);
			map = service.selectReservationDetail(map);
			return new ModelAndView("reservation_modifyT.sub", "reInfo",  map);
		}catch(Exception e){
			e.printStackTrace();
			return new ModelAndView("error.res","error_message", e.getMessage());
		}
	}
	//예약삭제
	@RequestMapping("removeReservation.do")
	public ModelAndView removeReservation(int reservationNo) throws Exception{
		try{
			service.removeReservation(reservationNo);
			return new ModelAndView("reserDelete_success.res");
		}catch(Exception e){
			e.printStackTrace();
			return new ModelAndView("error.res","error_message", e.getMessage());
		}
	}
	//예약현황 - 주인장
	@RequestMapping("my_reservation_owner_info.do")
	public ModelAndView selectMyReservationOwner(String memberId) throws Exception{
		try{
			ArrayList list = service.selectMyReservationOwner(memberId);
			return new ModelAndView("my_reservation_owner.sub", "list", list);
		}catch(Exception e){
			e.printStackTrace();
			return new ModelAndView("error.res","error_message", e.getMessage());
		}
	}
	//예약현황 상세정보 - 주인장
	@RequestMapping("reservation_detail_owner.do")
	public ModelAndView reservationDetailOwner(int roomNo, int reservationNo) throws Exception{
		try{
			HashMap map = new HashMap();
			map.put("roomNo", roomNo);
			map.put("reservationNo", reservationNo);
			map = service.selectReservationDetail(map);
			return new ModelAndView("reservation_modifyO.sub", "reInfo",  map);
		}catch(Exception e){
			e.printStackTrace();
			return new ModelAndView("error.res","error_message", e.getMessage());
		}
	}
	//예약정보수정- 주인장
	@RequestMapping("updateReservation.do")
	public ModelAndView modifyReservation(int reservationNo, String reservationCode, String memberId, int amount, HttpSession session) throws Exception{
		try{
			HashMap map = new HashMap();
			map.put("reservationNo", reservationNo);
			map.put("reservationCode", reservationCode);
			service.modifyReservation(map);
			//예약 완료시 amount 수정
			if(reservationCode.equals("Y")){
				HashMap map2 = new HashMap();
				map2.put("memberId", memberId);
				map2.put("amount", amount);
				MemberDTO mdto = (MemberDTO)session.getAttribute("login_info");
				mdto = service.modifyAmount(map2, mdto.getMember_id());
				session.setAttribute("login_info", mdto);
			}
			return new ModelAndView("reserModify_success.res");
		}catch(Exception e){
			e.printStackTrace();
			return new ModelAndView("error.res","error_message", e.getMessage());
		}
	}
	//예약 수락 취소시 상태 변경 & amount 변경
	@RequestMapping("updateReCodeandAmount.do")
	public ModelAndView modifyReCodeandAmount(int cost, String memberId, int reservationNo, HttpSession session) throws Exception{
		try {
			HashMap map = new HashMap();
			map.put("cost", cost);
			map.put("memberId", memberId);
			map.put("reservationNo", reservationNo);
			service.updateReCodeandAmount(map);
			
			HashMap map2 = new HashMap();
			map2.put("memberId", memberId);
			MemberDTO mdto = (MemberDTO)session.getAttribute("login_info");
			mdto = service.selectMyInfo(memberId);
			session.setAttribute("login_info", mdto);
			return new ModelAndView("reserModify_success.res");
		} catch (Exception e) {
			e.printStackTrace();
			return new ModelAndView("error.res","error_message", e.getMessage());
		}
	}
}
